﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownFileSplitter.Markdown
{
    /// <summary>
    /// 一个MarkdownFileContentItem是Markdown文件中的一块。
    /// 它包含章节等级，如一级、二级、三级。
    /// 它包含文档内容。
    /// 它包含子对象,比如一级MarkdownFileContentItem包含很多二级MarkdownFileContentItem
    /// </summary>
    internal class MarkdownFileContentItem
    {
        /// <summary>
        /// 层级
        /// </summary>
        public int Level { get; set; }
        public string Title { get; set; }
        /// <summary>
        /// 这部分内容的AI陈述
        /// </summary>
        public string AIDescription { get; set; }
        /// <summary>
        /// 开始的行号，即Markdown文档中，本对象开始的行号
        /// </summary>
        public int StartRowIndex { get; set; }
        /// <summary>
        /// 结束的行号，即Markdown文档中，本对象开始的结束
        /// </summary>
        public int EndRowIndex { get; set; }
        public List<MarkdownFileContentItem> Children { get; set; }

        public MarkdownFileContentItem(int level, string content, int startRowIndex, int endRowIndex)
        {
            Level = level;
            Title = content;
            StartRowIndex = startRowIndex;
            EndRowIndex = endRowIndex;
            Children = new List<MarkdownFileContentItem>();
        }

        public void AppendTo(string[] m_contentLines, StringBuilder markdownContent)
        {
            int startRow = this.StartRowIndex;
            int endRow = this.EndRowIndex;
            for (int i = startRow; i <= endRow; i++)
            {
                if (i >= m_contentLines.Length) continue;
                markdownContent.AppendLine(m_contentLines[i]);
            }
        }

        public void IncreaseLevel(MarkdownFileContent content)
        {
            var str = content.ContentLines[this.StartRowIndex];
            if (str.StartsWith("#"))
                content.ContentLines[this.StartRowIndex] = str.Substring(1);
            for (var i = 0; i < this.Children.Count; i++)
            {
                this.Children[i].IncreaseLevel(content);
            }
        }
        public void DescreaseLevel(MarkdownFileContent content)
        {
            content.ContentLines[this.StartRowIndex] = "#" + content.ContentLines[this.StartRowIndex];

            for(var i=0;i < this.Children.Count;i++)
            {
                this.Children[i].DescreaseLevel(content);
            }
        }
    }
}
